package group2assignone.model;

import group2assignone.dao.MyDAO;
import group2assignone.ucm.UCStartup;

/**
 * Ticket Class is an individual ticket, stores the flight number that the
 * user books.  Extends from TicketComponent.
 * @author group2
 */
public class Ticket extends TicketComponent {

    private String flightNum;
    private String username;
    private Flight f;
    private boolean booked = false;
    private static final int PERCENT = 100;

    public Ticket() {
    }

    /**
     * Constructor to take the username and flight number.
     * @param flightNum
     * @param username
     */
    public Ticket(String flightNum, String username) {
        this.flightNum = flightNum;
        this.username = username;
        getFlight();

    }

    /**
     * gets the flight from the database through MyDAO communication.
     */
    public void getFlight() {
        MyDAO coms = UCStartup.getInstance().getDAO();
        f = coms.sQLGetFlight(flightNum);
    }

    /**
     * Iterates through the ticket combination and books every ticket with
     * the bookingNum and itemNumber.
     * Returns the itemNumber (item within the booking).
     * @param bookingNum
     * @param itemNumber
     * @return
     */
    public int bookTicket(int bookingNum, int itemNum) {
        System.out.println("Ticket booked for:" + username + ". Flight: " + flightNum);
        MyDAO coms = UCStartup.getInstance().getDAO();
        booked = coms.sQLMakeBooking(this, bookingNum, itemNum);
        itemNum = itemNum + 1;
        return itemNum;
    }

    /**
     * Returns the total cost of the ticket.
     * @return
     */
    public double getCost() {
        double cost = 0;
        //Get cost from flight table
        cost = Double.valueOf(f.getCost());
        return cost;
    }

    /**
     * Returns the gst tax on the ticket as a double.
     * @return
     */
    public double getGST() {
        double gst = getCost() * GSTax.getInstanceGSTax().getGST() / PERCENT;

        return gst;
    }

    /**
     * returns the airport tax
     * @return
     */
    public double getAirportTax() {
        //Get departure location
        String depLoc = f.getDepLoc();
        MyDAO coms = UCStartup.getInstance().getDAO();
        double airportTax = getCost() * coms.sQLGetAirportTax(f.getDepLoc()) / PERCENT;
        return airportTax;
    }

    /**
     * Sets the ticket's flight number to the parameter flightNum
     * @param flightNum
     */
    public void setFlightNum(String flightNum) {
        this.flightNum = flightNum;
    }

    /**
     * Returns the ticket's flight number
     * @return
     */
    public String getFlightNum() {
        return flightNum;
    }

    /**
     * sets the ticket's username to the parameter user.
     * @param user
     */
    public void setUsername(String user) {
        username = user;
    }

    /**
     * returns the ticket's username
     * @return
     */
    public String getUsername() {
        return username;
    }

    @Override
    public boolean containsFlight(String flightNum) {
        if (flightNum.equals(this.flightNum)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns a boolean which indicated whether the ticket was successfully booked.
     * @return
     */
    public boolean isBooked() {
        return booked;
    }
}
